import {Component, OnInit} from '@angular/core';
import {LoadingService} from "../../../shared/loading.service";
import {AlertService} from "../../../shared/alert.service";
import {MessageService} from "../message.service"

@Component({
    selector: 'app-message-list',
    templateUrl: './message-list.component.html',
    styleUrls: ['./message-list.component.scss']
})
export class MessageListComponent implements OnInit {

    public dataArr: Array<any> = [];
    public pages: Number = 1;
    public isNodata: Boolean = false;
    public phone: any = '';
    public nickname: any = '';
    public state: any = 2;
    public keys: any = '';
    public states: any = '';

    public totalItems: any; // 列表数据总数
    public currentPage: any = 1; // 默认的开始页数
    public maxSize: Number = 3; // 显示的最大按钮数量

    constructor(public loading: LoadingService,
                public service: MessageService,
                public alert: AlertService) {
    }

    ngOnInit() {
        this.getData();
    }

    keyChanges() {
        this.phone = '';
        this.nickname = '';
    }

    // 获取列表数据
    getData() {
        this.loading.show();
        const params: any = {
            phone: this.phone,
            nickname: this.nickname,
            state: this.keys,
            page: this.pages,
        };
        console.log(params);
        this.service.getList(params).subscribe(
            res => {
                console.log(res, '数据');
                if (res['code'] === 0) {
                    this.isNodata = false;
                    if (res['data'].length === 0) {
                        this.isNodata = true;
                        this.dataArr = [];
                    } else {
                        this.dataArr = res['data'];
                        this.totalItems = res['count'];
                        for (let i = 0; i < this.dataArr.length; i++) {
                            this.dataArr[i]['add_time'] = this.formatDateTime(this.dataArr[i]['add_time'].substring(0, 10) * 1000);
                        }
                    }
                    this.loading.hide();
                } else {
                    this.loading.hide();
                    this.alert.show(res['msg'])
                }
            },
            error => {
                this.loading.hide();
                this.alert.show('网络错误');
            }
        );
    }

    // 阅读提示
    readings(id) {
        this.loading.show();
        const params: any = {
            id: id
        };
        this.service.update(params).subscribe(
            res => {
                if (res['code'] == 0) {
                    this.alert.show('操作成功');
                    setTimeout(() => {
                        this.getData();
                    }, 300);
                }
            }, error => {
                this.alert.show(error['msg']);
            }
        );
    }


    formatDateTime(inputTime) {
        const time = new Date(parseInt(inputTime));
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const min = m < 10 ? ('0' + m) : m;
        const d = time.getDate();
        const day = d < 10 ? ('0' + d) : d;
        const h = time.getHours();
        const hours = h < 10 ? ('0' + h) : h;
        const minute = time.getMinutes();
        const second = time.getSeconds();
        const minutes = minute < 10 ? ('0' + minute) : minute;
        const seconds = second < 10 ? ('0' + second) : second;
        // return y + '.' + min + '.' + day;
        return y + '-' + min + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    };

    // 分页
    pageChanged(event) {
        this.pages = event.page;
        this.loading.show();
        this.getData();
        setTimeout(() => {
            this.loading.hide();
        }, 300);
    }
}
